/* 自适应开始 */
console.log($(window).width());
if ($(window).width() < 760) {
    $('.bg').width($(window).width());
    $('.bg').height($(window).height());
} else {
    $('.bg').height($(window).height());
    $('.bg').width($('.bg').height() * .56);
}
window.onresize = function () {
    if ($(window).width() < 760) {
        $('.bg').width($(window).width());
        $('.bg').height($(window).height());
    } else {
        $('.bg').height($(window).height());
        $('.bg').width($('.bg').height() * .56);
    }
}
/* 自适应结束 */

/* 定义开始 */
var bg = $('.bg');
var flyMe = $('.flyMe');
var bulletBox = $('.bulletBox');
var foeBox = $('.foeBox');
var start = $('.start');
var restart = $('.restart');
var over = $('.gameover');
var scoreBox = $('.gameover span');
var score = 0;
var setBox = $('.set');
var setCon = $('.setCon');
var closeBox = $('.close');

// 背景
var bgObj = {
    x: 0,
}

// 飞机宽高
var flyMeObj = {
    flyMeW: parseInt(flyMe.width()),
    flyMeH: parseInt(flyMe.height()),
    x: 0,
    y: 0,
}

// 飞机能走到的最左和最右
var bgmarginL = parseInt(bg.css('margin-left')) + flyMeObj.flyMeW / 2;
var bgmarginR = parseInt(bg.width()) + bgmarginL - flyMeObj.flyMeW;

// 子弹
var bulletObj = {
    bullet: 0,
    x: 0,
    y: 0,
    width: 6,
    speed: 10,
    length: $('.bullet').parent().children().length,
}

// 敌机
var foeObj = {
    foe: 0,
    x: 0,
    y: 0,
    width: 34,
    height: 24,
    left: 0,
    speed: 10,
    length: 0,
}

// 标志
var flag = {
    touch: false,
    bulTop: 0,
    foeBottom: 0,
    gameoverF: false,
}

// 设置部分的按钮控制
var valueMap = new Map([
    ['bulCreate', '600'],
    ['foeCreate', '600'],
    ['speed', '600'],
]);

var bulCSpeed = $('.bulCreate').children('input').val();
console.log(bulCSpeed);
var foeCSpeed = $('.foeCreate').children('input').val();
console.log(foeCSpeed);
var Runspeed = $('.speed').children('input').val();
console.log(Runspeed);

/* 定义结束 */

/* 设置部分按钮的控制开始 */
function disabled(input) {
    if (input.val() <= 50) {
        input.siblings('.more').attr('disabled', false);
        input.siblings('.less').attr('disabled', true);
    } else {
        input.siblings('.less').attr('disabled', false);
    }
    if (valueMap.has(input.parent().attr('class'))) {
        if (parseInt(input.val()) >= parseInt(valueMap.get(input.parent().attr('class')))) {
            input.siblings('.more').attr('disabled', true);
        } else {
            input.siblings('.more').attr('disabled', false);
        }
    }
}

function moreLess() {
    $('.more').click(function () {
        value = parseInt($(this).siblings("input").val()) + 50;
        $(this).siblings("input").val(value);
        disabled($(this).siblings("input"));
    })
    $('.less').click(function () {
        value = parseInt($(this).siblings("input").val()) - 50;
        $(this).siblings("input").val(value);
        disabled($(this).siblings("input"));
    })
}
moreLess();

/* 监听input框内容是否改变 */
console.log($('.bulCreate input'));
$('.bulCreate input').bind('input propertychange', function () {
    console.log('bulCSpeed改变啦！！！' + $(this).val());
    if (parseInt($(this).val()) > parseInt(valueMap.get('bulCreate'))) {
        $('.bulCreate input').val(valueMap.get('bulCreate'));
    }
});
$('.foeCreate input').bind('input propertychange', function () {
    console.log('foeCSpeed改变啦！！！' + valueMap.get('foeCreate') + '    ' + $(this).val());
    if (parseInt($(this).val()) > parseInt(valueMap.get('foeCreate'))) {
        $('.foeCreate input').val(valueMap.get('foeCreate'));
    }
});
$('.speed input').bind('input propertychange', function () {
    console.log('Runspeed改变啦！！！' + $(this).val());
    if (parseInt($(this).val()) > parseInt(valueMap.get('speed'))) {
        $('.speed input').val(valueMap.get('speed'));
    }
});
/* 监听结束 */
/* 按钮的控制结束 */

// 跟随鼠标移动
function flyMemove(e) {
    // console.log(e.clientX, e.clientY);
    if (e.clientX > bgmarginL && e.clientX < bgmarginR && e.clientY > flyMeObj.flyMeH / 2 && e.clientY < bg
        .height() - flyMeObj.flyMeH / 2) {
        flyMe.css('transform', 'none');
        flyMe.css('left', (e.clientX - bgmarginL) + 'px');
        flyMe.css('top', (e.clientY - (flyMeObj.flyMeH / 2)) + 'px');
        // console.log((flyMe.css('left') + bgmarginL));
    }
}

// 创建子弹
function createBullet() {
    // 获取飞机位置
    bulletObj.x = parseInt(flyMe.css('left')) + flyMeObj.flyMeW / 2 - parseInt(bulletObj.width / 2) + 'px';
    bulletObj.y = flyMe.css('top');
    bulletObj.bullet = $(
        '<div class="bullet" style="background: url(https://s3.ax1x.com/2021/02/02/yuLi59.png); width: 6px; height: 14px; position: absolute; top: ' +
        bulletObj.y + '; left: ' + bulletObj.x + '"></div>'
    );
    bulletBox.append(bulletObj.bullet);
    // 子弹个数
    bulletObj.length = bulletBox.children().length - 1;
    // console.log('length: ' + bulletObj.length);
}

// 子弹动起来
function bulletGo() {
    $('.bullet').each(function () {
        // console.log(parseInt($(this).eq(0).css('top')));
        bulletObj.y = parseInt($(this).css('top')) - bulletObj.speed;
        $(this).css('top', bulletObj.y + 'px');
        flag.bulTop = parseInt($(this).css('top'));
        if (flag.bulTop < -5) {
            // console.log('触顶啦！！！');
            $(this).remove();
            // console.log($(this).parent().children().length);
        }
    })
}

// 创建敌机
function createPlane() {
    foeObj.x = randomLeft() + 'px';
    // foeObj.y = -(foeObj.height);
    foeObj.y = 0;
    // console.log('x: ' + foeObj.x);
    foeObj.foe = $(
        '<div class="foe" style="background: url(https://s3.ax1x.com/2021/02/02/yuLPUJ.png); width: 34px; height: 24px; position: absolute; top: ' +
        foeObj.y + 'px; left: ' + foeObj.x + '"></div>'
    );
    foeBox.append(foeObj.foe);
    foeObj.length = foeBox.children().length - 1;
    // console.log('length: ' + foeObj.length);
}

// 敌机动起来
function foeGo() {
    $('.foe').each(function () {
        // console.log(parseInt($(this).eq(0).css('top')));
        foeObj.y = parseInt($(this).css('top')) + foeObj.speed;
        $(this).css('top', foeObj.y + 'px');
        flag.foeBottom = parseInt($(this).css('top'));
        if (flag.foeBottom > parseInt(bg.height()) + foeObj.height) {
            // console.log('触底啦！！！');
            $(this).remove();
            // console.log($(this).parent().children().length);
        }
    })
}

// 获取对象位置
function getPosition(div) {
    var bounds = {};
    bounds.left = $(div).offset().left;
    bounds.right = $(div).offset().left + parseInt($(div).width());
    bounds.top = $(div).offset().top;
    bounds.bottom = $(div).offset().top + parseInt($(div).height());
    return bounds;
}

/* 碰撞，游戏失败 */
function crash(div1, div2) {
    var bounds1 = getPosition(div1);
    var bounds2 = getPosition(div2);
    if (bounds1.left >= bounds2.left && bounds1.left <= bounds2.right) {
        if (bounds1.top >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
            return true;
        } else if (bounds1.bottom >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
            return true;
        }
    } else if (bounds1.right >= bounds2.left && bounds1.right <= bounds2.right) {
        if (bounds1.top >= bounds2.top && bounds1.top <= bounds2.bottom) {
            return true;
        } else if (bounds1.bottom >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
            return true;
        }
    }
    return false;
}

// 时间间隔创建子弹和敌机
function createAll() {
    var BulCreate = setInterval(() => {
        // 子弹数目
        createBullet();
        // 子弹发射速度
    }, 710 - $('.bulCreate input').val());
    var foeCreate = setInterval(() => {
        createPlane();
        // 敌机数量
    }, 710 - $('.foeCreate input').val());

    // 子弹和敌机运动速度
    var BulGo = setInterval(() => {
        // 背景运动
        bgObj.x += 10;
        bg.css('background-positionY', bgObj.x + 'px');
        // 子弹运动
        bulletGo();
        // 敌机运动
        foeGo();
        // 碰撞
        $('.foe').each(function (index, foe) {
            if (crash(flyMe, foe) || crash(foe, flyMe)) {
                console.log('飞机与敌机碰撞');
                gameOver();
                // return;/
            }
            $('.bullet').each(function (index, bullet) {
                if (crash(foe, bullet) || crash(bullet, foe)) {
                    foe.remove();
                    bullet.remove();
                    // 分数！！！！
                    score++;
                    console.log(score);
                }
            })
        })
        // 飞行速度！！！！！
    }, 610 - $('.speed input').val());

    function gameOver() {
        console.log('游戏结束！！！！');
        flag.gameoverF = true;
        scoreBox.html(score);
        over.show();
        setBox.show();
        clearInterval(BulCreate);
        clearInterval(foeCreate);
        clearInterval(BulGo);
        bg.unbind();
    }
}

// 点击开始游戏
start.click(function () {
    start.hide();
    setBox.hide();
    createAll();
    bg.mousemove(flyMemove);
})

// 点击重新开始游戏
restart.click(function () {
    score = 0;
    foeBox.children().remove();
    bulletBox.children().remove();
    createAll();
    bg.mousemove(flyMemove);
    over.hide();
    setBox.hide();
})

// 随机数
function randomLeft() {
    var range = parseInt(bg.width()) - foeObj.width;
    return Math.floor(Math.random() * range);
}

// 关闭设置窗口
closeBox.click(function (event) {
    event.stopPropagation();
    setCon.hide();
    if (flag.gameoverF) {
        over.show();
    } else {
        start.show();
    }
})

// 设置按钮点击事件
setBox.click(function (event) {
    event.stopPropagation();
    setCon.show();
    start.hide();
    over.hide();
})